<?php
/**
 * 统计管理
 * @author Administrator
 */
class tongji extends admin{
    public function __construct(){
        parent::__construct();
    }
    
    public function index(){
        if(IS_AJAX){
            $coach_id=(int)$_GET['id'];//教练id
            $days=(int)$_GET['d'];//查询天数
            if($days<=0) $days=30;
            $start_day=date('Y-m-d');//开始日期
            $end_day=date('Y-m-d',strtotime('+'.($days-1).'day'));//结束日期
            /************计算出每天预约的课时及课时数*************/
            $yuyue=db()->getAll('select book_date,book_time,class_num from x_coach_book where coach_id='.$coach_id.' and book_date between "'.$start_day.'" and "'.$end_day.'" and status=0');
            $yuyue_data=array();
            foreach($yuyue as $k=>$v){
                if(isset($yuyue_data[$v['book_date']]['class_num'])) $yuyue_data[$v['book_date']]['class_num']+=$v['class_num'];
                else $yuyue_data[$v['book_date']]['class_num']=(int)$v['class_num'];
                if(isset($yuyue_data[$v['book_date']]['book_time'])) $yuyue_data[$v['book_date']]['book_time']+=','.$v['book_time'];
                else $yuyue_data[$v['book_date']]['book_time']=$v['book_time'];
            }
            //**********************************************
            //教练默认排班记录
            $default_pb=db()->getRow('select pb_time,pb_num from x_paiban where coach_id='.$coach_id.' and pb_type=1');
            if(!$default_pb) $default_pb='8,9,10,11,12,13,14,15,16,17,18';
            //教练请假记录
            $leave=db()->getAll('select begin_time,end_time from x_leave_record where uid='.$coach_id.' and status=1');
            $data=array();
            for($i=0;$i<$days;$i++){
                $d=date('Y-m-d',strtotime('+'.$i.'day'));
                $pb=db()->getRow('select pb_time,pb_num from x_paiban where coach_id='.$coach_id.' and pb_type=0 and  pb_date="'.$d.'"');//获取当前日期排班记录
                if(!$pb) $pb=$default_pb;//默认排班
                //***********计算请假课时***************************
                $qj=0;//请假
                $pb_time=explode(',',$pb['pb_time']);
                foreach($pb_time as $v){
                    $v_time=strtotime($d.' '.$v.':00');
                    if($leave){
                        foreach($leave as $l){
                            if($v_time>=$l['begin_time']&&$v_time<$l['end_time']) {
                                $qj++;
                                break;
                            }
                        }
                    }
                   
                    
                }
                //************计算预约后教练请假的课时数********************
                $cf=0;//预约后又请假的重复课时数
                $book_time=explode(',',$yuyue_data[$d]['book_time']);
                foreach($book_time as $v){
                    $v_time=strtotime($d.' '.$v.':00');
                    if($leave){
                        foreach($leave as $l){
                            if($v_time>=$l['begin_time']&&$v_time<$l['end_time']) {
                                $cf++;
                                break;
                            }
                        }
                    }
                }
                //**************生成json数据************************************
                $data[$i][]=$d;//日期
                if(array_key_exists($d,$yuyue_data)) {
                    $data[$i][]=$pb['pb_num']-($yuyue_data[$d]['class_num']+$qj-$cf);//可预约
                    $data[$i][]=$yuyue_data[$d]['class_num'];//已预约
                    $data[$i][]=$qj;//请假
                }else{
                    $data[$i][]=$pb['pb_num']-$qj;//可预约
                    $data[$i][]=0;//已预约
                    $data[$i][]=$qj;//请假
                }
               //******************************************************** 
            }
            exit(json_encode($data));
           
        }else{
            $list=db()->getAll('select id,real_name,mobile from x_user where user_type=2');
            $tpl=$this->Template();
            $tpl->assign('list',$list);
            $tpl->display();
        }
        
    }
    
    public function dayinfo(){
        $date=$_GET['d'];
        $coach_id=(int)$_GET['id'];
        
        //查询教练排班信息
        $pb_time=db()->getOne('select pb_time from x_paiban where coach_id='.$coach_id.' and pb_type=0 and pb_date="'.$date.'"');//当前日期排班记录
        if(!$pb_time){
            $pb_time=db()->getOne('select pb_time from x_paiban where coach_id='.$coach_id.' and pb_type=1');//默认排班
            $pb_time=$pb_time!==false?$pb_time:'8,9,10,11,12,13,14,15,16,17,18';
        }
        
        //查询请假信息
        $begin_timestamp=strtotime($date);//当天开始时间戳
        $end_timestamp=$begin_timestamp+3600*24;//当天结束时间戳
        $leave=db()->getAll('select begin_time,end_time from x_leave_record where uid='.$coach_id.' and status=1 and ('.$begin_timestamp.' between begin_time and end_time OR '.$end_timestamp.' between begin_time and end_time OR begin_time between '.$begin_timestamp.' and '.$end_timestamp.')');//请假记录
        
        //var_dump($leave);
        $yuyue=db()->getAll('select book_time from x_coach_book where coach_id='.$coach_id.' and book_date="'.$date.'" and status=0');//预约记录
        $yuyue_time_all=array();
        if($yuyue) foreach($yuyue as $t){$yuyue_time_all[]=$t['book_time'];}
       
        $pb=explode(',',$pb_time);
        $data=array();
        foreach($pb as $k=>$v){
            $data[$k]['time']=$v.':00';
            $data[$k]['type']=0;//可预约
            $v_time=strtotime($date.' '.$v.':01');
            if(in_array($v,$yuyue_time_all)){
                $data[$k]['type']=1;//已预约
            }
            if($leave){
                foreach($leave as $l){
                    if($v_time>$l['begin_time']&&$v_time<$l['end_time']) {
                        $data[$k]['type']+=2;//请假
                        break;
                    }
                }
            } 
            
        }
        $tpl=$this->Template();
        $tpl->assign('list',$data);
        $tpl->display();
        //*********************************
    }
    
}